<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<html>
<head>
	<title>吉他指板音游戏</title>
	<style type="text/css">
	body{background-color:#f7f3f0}
	div{font-size:20px;}
	li{font-size:20px;list-style:none;}
	input{padding:5px;font-size:20px;}
	pre{margin:0;font-family:ya;}
	#container{width:1030px;}
	#main{height:300px;background-image:url(images/bg.png);}
	#finger{width:26px;height:26px;line-height:26px;font-weight:bold;text-align:center;color:white;border-radius:50%;position:absolute;}
	#s6{width:97px;height:81px;background-image:url(images/6.png);float:left}
	#s6 div{width:84px;height:70px;margin:6px;background-color:gray;position:absolute;opacity:30%;}
	#s6 span{height:14px;line-height:14px;background-color:white;display:block;margin-left:40px;margin-top:0px;}
	#qsn{height:90px;margin:10px 0 10px 40px;padding-top:10px;background-color:white;border-radius:5px;}
	#qsn input{width:50px;}
	#setting{margin:10px 0 10px 40px;padding:10px;background-color:white;border-radius:5px;}
	#record{height:400px;margin-left:40px;padding:5px;background-color:white;border-radius:5px;overflow-y:auto;}
	</style>
</head>
<body>
	<div id="container">
		<div id="main">
			<div id="finger"></div>
		</div>
		<div id="qsn">
			<div id="s6"><div></div><span>0</span></div>
			<div id="qs"></div>
			<div id="op">
				<input type="button" value="1" onclick="choose(1);">
				<input type="button" value="2" onclick="choose(2);">
				<input type="button" value="3" onclick="choose(3);">
				<input type="button" value="4" onclick="choose(4);">
				<input type="button" value="5" onclick="choose(5);">
				<input type="button" value="6" onclick="choose(6);">
				<input type="button" value="7" onclick="choose(7);">
			</div>
		</div>
		<div id="setting">
			<input type="button" value="全部" onclick="partArr('all');">
			|
			<input type="button" value="Mi型音阶" onclick="partArr('mi');">
			<input type="button" value="La型音阶" onclick="partArr('la');">
			<input type="button" value="Re型音阶" onclick="partArr('re');">
			|
			<input type="button" value="1弦" onclick="partArr('1');">
			<input type="button" value="2弦" onclick="partArr('2');">
			<input type="button" value="3弦" onclick="partArr('3');">
			<input type="button" value="4弦" onclick="partArr('4');">
			<input type="button" value="5弦" onclick="partArr('5');">
			<input type="button" value="6弦" onclick="partArr('6');">
		</div>
		<div id="record"></div>
	</div>
	<script type="text/javascript">
	/*********************/
	/*     公用方法      */
	/*********************/
	function $(id){
		return document.getElementById(id);
	}
	function show(obj){
		obj.style.display = 'block';
	}
	function hide(obj){
		obj.style.display = 'none';
	}
	function blink(obj,count){
		var cnt = 1;
		var timer = setInterval(function(){
			if(cnt++%2==1){
				show(obj);
			}else{
				hide(obj);
			}
			if(cnt==count*2)clearInterval(timer);
		},100);
	}
	function log(...s){
		console.log(...s);
	}
	function rollFinger(arr){
		var x = Math.floor(Math.random() * arr.length);
		var y = Math.floor(Math.random() * arr[0].length);
		var obj = {x:x,y:y};
		
		if(arr[x][y]==0 || JSON.stringify(obj)==JSON.stringify(old)){
			return rollFinger(arr);
		}else{
			old = obj;
			return obj;
		}
	}
	function fillArr(cnt){
		var arrBlank = [0,0,0,0,0,0,0,0,0,0,0,0,0,0];
		while(--cnt>0){
			arr.push(arrBlank);
		}
	}
	function cutArr(x0,x1,y0,y1){
		for(var i=x0;i<x1;i++){
			var arrTemp = [];
			for(var j=0;j<=y1;j++){
				arrTemp[j] = arrOri[i][j];
				if(j<y0)arrTemp[j] = 0;
			}
			arr.push(arrTemp);
		}	
	}
	
	/*********************/
	/*     游戏逻辑      */
	/*********************/
	//定义变量
	var finger = $('finger');
	var s6 = $('s6');
	var s6span = $('s6').getElementsByTagName('span')[0];
	var s6div = $('s6').getElementsByTagName('div')[0];
	var qs = $('qs');
	var op = $('op');
	var arrOri = [
		[3,4,0,5,0,6,0,7,1,0,2,0,3,4],
		[7,1,0,2,0,3,4,0,5,0,6,0,7,1],
		[5,0,6,0,7,1,0,2,0,3,4,0,5,0],
		[2,0,3,4,0,5,0,6,0,7,1,0,2,0],
		[6,0,7,1,0,2,0,3,4,0,5,0,6,0],
		[3,4,0,5,0,6,0,7,1,0,2,0,3,4],
	];
	var arr = arrOri;
	var f,x,y,value,old,unlock;
	/**
	 *	初始化
	 */
	function init(){
		log(arr);
		//隐藏指板音和问题
		hide(finger);
		hide(s6);
		hide(qs);
		hide(op);
		
		//重置指板音
		finger.style.backgroundColor = "blue";
		finger.innerText = '?';
		
		//生成问题
		answer();
	}
	/**
	 *	生成答案并展示音符和问题
	 */
	function answer(){
		//生成答案
		f = rollFinger(arr);
		x = f.x;
		y = f.y;
		value = arr[x][y];
		finger.style.top = 46+38*x+'px';
		var offset = 0;
		if(y==0)offset = 20;
		if(y==13)offset = -20;
		finger.style.left = offset+32+76*y+'px';
		log('x:',x,', y:',y);
		log('音:',value);

		//展示音符
		blink(finger,3);
		
		//展示6线谱
		s6span.innerText = y;
		s6span.style.marginTop = 13*x+'px';
		s6span.style.width = 14+'px';
		if(y<4){
			s6div.style.backgroundColor = '#808080';
		}else if(y>=4 && y<9){
			s6div.style.backgroundColor = 'red';
		}else if(y>=9){
			s6div.style.backgroundColor = '#fa00cc';
		}
		if(y>9){
			s6span.style.width = 26+'px';
			s6span.style.marginLeft = 36+"px";
		}else{
			s6span.style.width = 14+'px';
			s6span.style.marginLeft = 42+"px";
		}
		
		//展示问题
		qs.innerText = (x+1)+' 弦 '+y+' 品是什么音?';
		setTimeout(function(){
			show(s6);
			show(qs);
			show(op);
			unlock = true;
		},1000);
	}
	
	/**
	 *	选择选项
	 */
	function choose(v){
		if(!unlock) return;
		unlock = false;
		
		var right = false;
		var color = "red";
		if(v==value){
			color = "green";
			right = true;
		}
		finger.innerText = value;
		
		//添加记录
		$('record').innerHTML = '<pre style="color:'+color+'">'+(x+1)+'弦'+y+'品&#9===> '+value+'</pre>' + $('record').innerHTML;

		var timer = 2000;
		if(!right)timer = 3000;
		setTimeout(function(){
			init();
		},timer);
	}
	
	/**
	 *	选择部分
	 */
	function partArr(p){
		arr = [];
		
		if(p=='all'){
			arr = arrOri;
		}else if(p=='mi'){
			cutArr(0,6,0,3);
		}else if(p=='la'){
			cutArr(0,6,4,8);
		}else if(p=='re'){
			cutArr(0,6,9,13);
		}else if(p>=1 || p<=6){
			fillArr(p);
			cutArr(p-1,p,0,13);
		}
		init();
	}

	//初始化
	init();

	//监听按键
	document.addEventListener('keydown', function(event) {
		var key = event.key;
		if(key>=1 && key<=7){
			choose(key);
		}
	});

	</script>
</body>
</html>



